package com.company.cloud.user.modules.app.controller;

import com.company.cloud.captcha.modules.service.CaptchaService;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.user.modules.app.controller.req.UserLoginByName2Pass2CodeParam;
import com.company.cloud.user.modules.app.service.UserCompanyService;
import com.company.cloud.user.modules.app.service.UserLoginService;
import com.company.cloud.user.modules.base.entity.UserInfo;
import com.company.cloud.user.modules.bus.producer.UserProducer;
import com.company.cloud.user.modules.cache.service.UserPermissionCacheService;
import com.company.cloud.user.modules.cache.utils.UserConvertUtils;
import com.company.common.server.config.cloud.ProjectConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户（用户名+密码+验证码)登录
 */
@Log4j2
@RestController
@RequestMapping("/app/user/login")
@Api(tags = "UC_用户用户名密码登录")
public class UserLogin2NamePassController {

    @Autowired
    private CaptchaService captchaService;
    @Autowired
    private UserCompanyService userCompanyService;
    @Autowired
    private UserLoginService userLoginService;
    @Autowired
    private UserPermissionCacheService userPermissionCacheService;
    @Autowired
    private ProjectConfig projectConfig;
    @Autowired
    private UserProducer userProducer;


    @PostMapping("/loginByName2Password2Code")
    @ApiOperation(value = "(用户名+密码+验证码)登录",response = LoginUserInfo.class)
    public BaseResult loginByName2Password2Code(@RequestBody UserLoginByName2Pass2CodeParam params) {
        ValidatorUtils.validate(params);
        //新的图形验证码上
        captchaService.verification(UserConvertUtils.getCaptcha2VO(params.getCaptchaVerification()));
        String companyNo=userCompanyService.companyCheck(AuthContextUtils.getCompanyNo());
        UserInfo userInfo = userLoginService.loginByUserName(companyNo, params.getUserName(), params.getPassword());
        LoginUserInfo data= userLoginService.login(userInfo);
        userPermissionCacheService.loadPermissionRoleCache(data.getCompanyNo());
        userProducer.userLogin("用户名密码验证码登录",params,data);
        return BaseResult.ok(data);
    }



    /**
     * 只适用平台模式(不需要公司号)
     * */
    @PostMapping("/loginByName2Password2CodeNoComNo")
    @ApiOperation(value = "(用户名+密码+验证码)登录",response = LoginUserInfo.class)
    public BaseResult loginByName2Password2CodeNoComNo(@RequestBody UserLoginByName2Pass2CodeParam params) {
        ValidatorUtils.validate(params);
        //新的图形验证码上
        captchaService.verification(UserConvertUtils.getCaptcha2VO(params.getCaptchaVerification()));
        UserInfo userInfo = userLoginService.loginByUserName("", params.getUserName(), params.getPassword());
        LoginUserInfo data= userLoginService.login(userInfo);
        userPermissionCacheService.loadPermissionRoleCache(data.getCompanyNo());
        userProducer.userLogin("用户名密码验证码登录",params,data);
        return BaseResult.ok(data);
    }






}
